package com.shootforthestars.objects;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;

import com.shootforthestars.core.Actor;

public class ObstacleSystem implements Actor {
	private List<Obstacle> obstacles;
	private Rocket rocket;

	public ObstacleSystem(Rocket rocket) {
		obstacles = new ArrayList<Obstacle>();
		this.rocket = rocket;
	}

	@Override
	public void init() throws SlickException {
		for (Obstacle o : obstacles) {
			o.init();
		}
	}

	@Override
	public void render(Graphics graphics) throws SlickException {
		for (Obstacle o : obstacles) {
			o.render(graphics);
		}
	}

	@Override
	public void update(Input input, int delta) throws SlickException {
		Iterator<Obstacle> it = obstacles.iterator();
		while (it.hasNext()) {
			Obstacle obstacle = it.next();
			obstacle.update(input, delta);
			if (rocket.getCollisionComponent().collidesWith(
					obstacle.getCollisionComponent())) {
				obstacle.onHitRocket(rocket);
				if (obstacle.isAlive() == false) {
					it.remove();
				}
			}
		}
	}

	public boolean addObstacle(Obstacle obstacle) {
		return obstacles.add(obstacle);
	}
}
